package com.jochen.core.screen

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.ScreenAdapter
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
import com.badlogic.gdx.utils.viewport.FillViewport
import com.jochen.core.assets.*
import com.jochen.core.audio.AudioManager
import com.jochen.core.screen.transitions.FadeTransition
import com.jochen.core.ui.onClick
import com.jochen.core.ui.realize.RMenuBackground
import com.jochen.core.world.GdxGame

class Menu(
    game: GdxGame,
    screenManager: ScreenManager) : BaseScreen(game,screenManager) {
    private val audioManager: AudioManager = game.audioManager
   
    init {
        initUi()
    }

    private fun initUi() {
        val sk = game.assetService.get(SkinAsset.DEFAULT)
        val table = Table(sk)
        table.setFillParent(true)

        val textureAtlas = game.assetService.get(AtlasAsset.MENU)
        addActor(RMenuBackground(textureAtlas))

        val gameInit = ImageButton(TextureRegionDrawable(textureAtlas.findRegion("120")))
        val settings = ImageButton(TextureRegionDrawable(textureAtlas.findRegion("142")))
        val exit = ImageButton(TextureRegionDrawable(textureAtlas.findRegion("143")))

        gameInit.onClick{
            audioManager.playSound(SoundAsset.BUTTON_PRESS)
            setScreen(GameScreen::class, FadeTransition::class)
        }

        settings.onClick {
            audioManager.playSound(SoundAsset.BUTTON_PRESS)
            setScreen(SettingsScreen::class, FadeTransition::class)
        }

        exit.onClick {
            audioManager.playSound(SoundAsset.BUTTON_PRESS)
            Gdx.app.exit()
        }

        val topBar = Table()
        topBar.defaults().height(50f)
        topBar.add(settings).left()
        topBar.add().expandX()
        topBar.add(exit).right()

        table.top().add(topBar).growX().padTop(50f).row()
        table.add().expandY().row()
        table.add(gameInit).width(200f).height(200f).row()
        table.add().expandY()

        addActor(table)
        //stage.isDebugAll = true
        audioManager.playMusic(MusicAsset.MENU)
    }

    override fun update(delta: Float) {
    }

}